package com.tfswx.jkgl.core.cscl;

import com.alibaba.fastjson.JSON;
import com.tfswx.jkgl.core.cscl.base.CsclBase;
import com.tfswx.jkgl.core.task.ServiceTask;
import com.tfswx.jkgl.core.task.ServiceTaskContextHolder;
import com.tfswx.jkgl.core.utils.MyScriptEngine;
import com.tfswx.jkgl.core.utils.ServiceUtil;
import lombok.Data;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ricky
 * @date 2021/12/27
 */
@Slf4j
@Component
public class ConfirmFile implements CsclBase {
    @Override
    public String getName() {
        return "文件确认";
    }

    @Override
    public boolean sfxycs() {
        return true;
    }

    @Override
    public Object encode(Object value, Object jmcs, String pBds, String id) {
        log.info("{}需要进行文件确认", pBds);
        if (jmcs == null) {
            throw new RuntimeException("策略参数不能为空");
        }
        CsParam csParam;
        try {
            csParam = JSON.parseObject(jmcs.toString(), CsParam.class);
        } catch (Exception e) {
            throw new RuntimeException("策略参数类型", e);
        }

        // 创建后置任务
        ConfirmTask confirmTask = new ConfirmTask();

        if (value == null) {
            confirmTask.setWjbhs(new ArrayList<>());
        } else if (value instanceof List) {
            confirmTask.setWjbhs((List<String>) value);
        } else if (value instanceof String) {
            if (StringUtils.isBlank(value.toString())) {
                confirmTask.setWjbhs(new ArrayList<>());
            } else {
                confirmTask.setWjbhs(Collections.singletonList(value.toString()));
            }
        } else {
            throw new RuntimeException(getName() + "策略参数类型错误");
        }
        confirmTask.setCsParam(csParam);
        confirmTask.setZybmBdsQz(pBds);
        ServiceTaskContextHolder.addTask(confirmTask, id);
        return value;
    }

    public static class ConfirmTask extends ServiceTask {
        private final String serviceName = "ywwjServiceImpl";
        private final String methodName = "confirm";

        @Setter
        private String zybmBdsQz;

        @Setter
        private CsParam csParam;
        @Setter
        private List<String> wjbhs;

        public Object getParam(Object dto) {
            MyScriptEngine myScriptEngine = new MyScriptEngine();
            int i = zybmBdsQz.lastIndexOf(".");
            String newZybmBds = csParam.zybmBds;
            if (i != -1) {
                String bdsQz = zybmBdsQz.substring(0, i);
                String[] split = csParam.zybmBds.split("\\+");
                for (int j = 0; j < split.length; j++) {
                    String s = split[j];
                    split[j] = bdsQz + "." + s.trim();
                }
                newZybmBds = String.join("+", split);
            }
            log.info(newZybmBds);
            String zybm = String.valueOf(myScriptEngine.eval(newZybmBds, dto));
            log.info("资源编码：{}", zybm);
            Map<String, Object> map = new HashMap<>();
            map.put("zybm", zybm);
            map.put("mkbm", csParam.mkbm);
            map.put("zylxbm", csParam.zylxbm);
            map.put("wjbhs", wjbhs);
            return map;
        }

        @Override
        public void run(Object dto) {
            ServiceUtil.excuteMethod(serviceName, methodName, getParam(dto));
        }
    }

    @Data
    public static class CsParam {
        // {"zylxbm":"fwbtp","mkbm":"xwzx","zybmBds":"xxbh"}
        private String zylxbm;
        private String mkbm;
        private String zybmBds;
    }
}
